home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 6LXE80 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  8.7 KB  |  220 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.AbstractButton;
  4. import com.sun.java.swing.ButtonModel;
  5. import com.sun.java.swing.Icon;
  6. import com.sun.java.swing.JComponent;
  7. import com.sun.java.swing.LookAndFeel;
  8. import com.sun.java.swing.SwingUtilities;
  9. import com.sun.java.swing.UIManager;
  10. import com.sun.java.swing.plaf.ButtonUI;
  11. import com.sun.java.swing.plaf.ComponentUI;
  12. import java.awt.Color;
  13. import java.awt.Component;
  14. import java.awt.Dimension;
  15. import java.awt.Font;
  16. import java.awt.FontMetrics;
  17. import java.awt.Graphics;
  18. import java.awt.Insets;
  19. import java.awt.Rectangle;
  20. import java.io.Serializable;
  21.  
  22. public class BasicButtonUI extends ButtonUI implements Serializable {
  23.    private static final Insets defaultMargin = new Insets(2, 14, 2, 14);
  24.    private static final int defaultTextIconGap = 4;
  25.    private int shift_offset = 0;
  26.    protected static ButtonUI buttonUI;
  27.    protected BasicButtonListener listener;
  28.  
  29.    protected BasicButtonListener createListener(JComponent c) {
  30.       return new BasicButtonListener((AbstractButton)c);
  31.    }
  32.  
  33.    public static ComponentUI createUI(JComponent c) {
  34.       if (buttonUI == null) {
  35.          buttonUI = new BasicButtonUI();
  36.       }
  37.  
  38.       return buttonUI;
  39.    }
  40.  
  41.    public Insets getDefaultMargin(AbstractButton b) {
  42.       return defaultMargin;
  43.    }
  44.  
  45.    public int getDefaultTextIconGap(AbstractButton b) {
  46.       return 4;
  47.    }
  48.  
  49.    protected Color getDisabledColor() {
  50.       return UIManager.getColor("Button.disabled");
  51.    }
  52.  
  53.    protected Color getDisabledTextColor() {
  54.       return UIManager.getColor("Button.disabledText");
  55.    }
  56.  
  57.    protected Color getFocusColor() {
  58.       return UIManager.getColor("Button.focus");
  59.    }
  60.  
  61.    public Dimension getMaximumSize(JComponent c) {
  62.       return this.getPreferredSize(c);
  63.    }
  64.  
  65.    public Dimension getMinimumSize(JComponent c) {
  66.       return this.getPreferredSize(c);
  67.    }
  68.  
  69.    public Dimension getPreferredSize(JComponent c) {
  70.       AbstractButton b = (AbstractButton)c;
  71.       return BasicGraphicsUtils.getPreferredButtonSize(b, 4);
  72.    }
  73.  
  74.    protected Color getSelectColor() {
  75.       return UIManager.getColor("Button.pressed");
  76.    }
  77.  
  78.    private int getTextShiftOffset() {
  79.       return this.shift_offset;
  80.    }
  81.  
  82.    protected void installDefaults(JComponent c) {
  83.       c.setOpaque(false);
  84.       LookAndFeel.installColorsAndFont(c, "Button.background", "Button.foreground", "Button.font");
  85.       LookAndFeel.installBorder(c, "Button.border");
  86.    }
  87.  
  88.    protected void installKeyboardActions(JComponent c) {
  89.       this.listener.setupKeyboard((AbstractButton)c);
  90.    }
  91.  
  92.    protected void installListeners(JComponent c) {
  93.       if (this.listener == null) {
  94.          this.listener = this.createListener(c);
  95.       }
  96.  
  97.       ((Component)c).addMouseListener(this.listener);
  98.       ((Component)c).addMouseMotionListener(this.listener);
  99.       ((Component)c).addFocusListener(this.listener);
  100.       ((AbstractButton)c).addChangeListener(this.listener);
  101.    }
  102.  
  103.    public void installUI(JComponent c) {
  104.       this.installDefaults(c);
  105.       this.installListeners(c);
  106.       this.installKeyboardActions(c);
  107.    }
  108.  
  109.    public void paint(Graphics g, JComponent c) {
  110.       AbstractButton b = (AbstractButton)c;
  111.       ButtonModel model = b.getModel();
  112.       Dimension size = ((Component)b).getSize();
  113.       FontMetrics fm = g.getFontMetrics();
  114.       Insets i = c.getInsets();
  115.       Rectangle viewRect = new Rectangle(size);
  116.       viewRect.x += i.left;
  117.       viewRect.y += i.top;
  118.       viewRect.width -= i.right + viewRect.x;
  119.       viewRect.height -= i.bottom + viewRect.y;
  120.       Rectangle iconRect = new Rectangle();
  121.       Rectangle textRect = new Rectangle();
  122.       Font f = ((Component)c).getFont();
  123.       g.setFont(f);
  124.       String text = SwingUtilities.layoutCompoundLabel(fm, b.getText(), b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, b.getText() == null ? 0 : 4);
  125.       this.setTextShiftOffset(0);
  126.       if (model.isArmed() && model.isPressed()) {
  127.          this.paintButtonPressed(g, b);
  128.       }
  129.  
  130.       if (b.getIcon() != null) {
  131.          this.paintIcon(g, c, iconRect);
  132.       }
  133.  
  134.       if (text != null && !text.equals("")) {
  135.          this.paintText(g, c, textRect, text);
  136.       }
  137.  
  138.       if (b.isFocusPainted() && ((JComponent)b).hasFocus()) {
  139.          this.paintFocus(g, b, viewRect, textRect, iconRect);
  140.       }
  141.  
  142.    }
  143.  
  144.    protected void paintButtonPressed(Graphics g, AbstractButton b) {
  145.    }
  146.  
  147.    protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
  148.    }
  149.  
  150.    protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
  151.       AbstractButton b = (AbstractButton)c;
  152.       ButtonModel model = b.getModel();
  153.       Icon icon = null;
  154.       if (!model.isEnabled()) {
  155.          icon = b.getDisabledIcon();
  156.       } else if (model.isPressed() && model.isArmed()) {
  157.          icon = b.getPressedIcon();
  158.          if (icon == null) {
  159.             icon = b.getIcon();
  160.          } else {
  161.             this.setTextShiftOffset(0);
  162.          }
  163.       } else if (b.isRolloverEnabled() && model.isRollover()) {
  164.          icon = b.getRolloverIcon();
  165.       }
  166.  
  167.       if (icon == null) {
  168.          icon = b.getIcon();
  169.       }
  170.  
  171.       if (model.isPressed() && model.isArmed()) {
  172.          icon.paintIcon(c, g, iconRect.x + this.getTextShiftOffset(), iconRect.y + this.getTextShiftOffset());
  173.       } else {
  174.          icon.paintIcon(c, g, iconRect.x, iconRect.y);
  175.       }
  176.  
  177.    }
  178.  
  179.    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  180.       AbstractButton b = (AbstractButton)c;
  181.       ButtonModel model = b.getModel();
  182.       FontMetrics fm = g.getFontMetrics();
  183.       if (model.isEnabled()) {
  184.          g.setColor(((Component)b).getForeground());
  185.          BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x + this.getTextShiftOffset(), textRect.y + fm.getAscent() + this.getTextShiftOffset());
  186.       } else {
  187.          g.setColor(((Component)b).getBackground().brighter());
  188.          BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  189.          g.setColor(((Component)b).getBackground().darker());
  190.          BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x - 1, textRect.y + fm.getAscent() - 1);
  191.       }
  192.  
  193.    }
  194.  
  195.    protected void setTextShiftOffset(int i) {
  196.       this.shift_offset = i;
  197.    }
  198.  
  199.    protected void uninstallDefaults(JComponent c) {
  200.       LookAndFeel.uninstallBorder(c);
  201.    }
  202.  
  203.    protected void uninstallKeyboardActions(JComponent c) {
  204.       c.resetKeyboardActions();
  205.    }
  206.  
  207.    protected void uninstallListeners(JComponent c) {
  208.       ((Component)c).removeMouseListener(this.listener);
  209.       ((Component)c).removeMouseMotionListener(this.listener);
  210.       ((Component)c).removeFocusListener(this.listener);
  211.       ((AbstractButton)c).removeChangeListener(this.listener);
  212.    }
  213.  
  214.    public void uninstallUI(JComponent c) {
  215.       this.uninstallKeyboardActions(c);
  216.       this.uninstallListeners(c);
  217.       this.uninstallDefaults(c);
  218.    }
  219. }
  220.